package com.dl.mall.controller.admin;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.fastjson.JSONObject;
import com.dl.mall.annotation.MethodInfo;
import com.dl.mall.annotation.SaAdminCheckRole;
import com.dl.mall.entity.Page;
import com.dl.mall.entity.user.User;
import com.dl.mall.service.admin.UserService_Admin;
import com.dl.mall.utils.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author DL
 * @create 2022-03-30 11:40
 */
@Slf4j
@RestController()
@RequestMapping("/admin/user")
@SaAdminCheckRole(value = {"super","admin"})
@Api(tags="管理员用户controller")
public class UserController_Admin {

    @Resource
    private UserService_Admin userServiceAdmin;

    /**
     * 根据 用户id 修改账号状态
     * @return 用户列表
     */
    @ApiOperation(value="修改账号状态",notes="根据 用户id 修改账号状态")
    @MethodInfo
    @GetMapping("/modifyUserState")
    public Result modifyUserState(String userId, int state){
        boolean modifyUserState = userServiceAdmin.modifyState(userId, state);
        if (modifyUserState){
            return Result.ok("修改成功");
        }else {
            return Result.error("修改失败");
        }
    }

    @ApiOperation(value="修改账号信息",notes="根据 用户id 修改用户信息")
    @MethodInfo
    @PostMapping("/modifyUserInfo")
    public Result modifyUserInfo(@RequestBody User user){
        boolean modifyUserInfo = userServiceAdmin.modifyUserInfo(user);
        if (modifyUserInfo){
            return Result.ok("修改成功");
        }else {
            return Result.error("修改失败");
        }
    }

    @ApiOperation(value="查询用户",notes="根据 用户id 查询用户信息")
    @MethodInfo
    @GetMapping("/getUserById")
    public Result getUserById(String userId){
        System.out.println("userId = " + userId);
        User user = userServiceAdmin.getUserByUserId(userId);
        if (user != null){
            return Result.ok("查询用户成功").data("user", user);
        }else {
            return Result.error("查询用户失败");
        }
    }

    @ApiOperation(value="根据 条件 查询用户列表",notes="根据 条件 查询用户列表")
    @MethodInfo
    @PostMapping("/list")
    @SentinelResource(value = "limiting")
    public Result getUserList(@RequestBody JSONObject jsonObject){
        System.out.println("jsonObject = " + jsonObject);
        JSONObject condition = jsonObject.getJSONObject("condition");

        Page<User> page = userServiceAdmin.getUserByCondition(condition);
        if (page != null){
            return Result.ok("查询用户列表成功").data("page", page);
        }else {
            return Result.error("查询用户列表失败");
        }
    }

    @ApiOperation(value="重置用户账号密码为123")
    @GetMapping("/resetPassword")
    private Result resetPassword(String userId) {
        String resetPassword = userServiceAdmin.resetPassword(userId);
        if (resetPassword != null){
            return Result.ok("成功").data("resetPassword", resetPassword);
        }else {
            return Result.error("失败");
        }
    }


}
